//
//  StatusToolBar.swift
//  开心微博
//
//  Created by 郑开心 on 15/12/13.
//  Copyright © 2015年 郑开心. All rights reserved.
//

import UIKit

class StatusToolBar: UIImageView {

    lazy var btns : [UIButton] = {
        let buttons = [UIButton]()
        return buttons
    }()
    
    lazy var divideVs : [UIImageView] = {
        let divideVs = [UIImageView]()
        return divideVs
    }()
    
    var statusFrame : StatusFrame!{
        didSet{
           setStatus()
        }
    }
    
    var retweet : UIButton!
    var comment : UIButton!
    var unlike : UIButton!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpAllChindView()
        self.userInteractionEnabled = true
        self.image = UIImage.imageWithStretchableName("timeline_card_bottom_line_highlighted")
        
        // self.backgroundColor = UIColor.redColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpAllChindView(){
        
        retweet = setOneButton("转发", imageName: "timeline_icon_retweet")
        comment = setOneButton("评论", imageName: "timeline_icon_comment")
        unlike = setOneButton("赞", imageName: "timeline_icon_unlike")
        
        for _ in 0..<2{
            let divideV = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
            divideVs.append(divideV)
            self.addSubview(divideV)
        }
    }
    
    
    func setOneButton(title: String,imageName: String) -> UIButton{
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle(title, forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        btn.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(12)
        btn.titleEdgeInsets = UIEdgeInsetsMake( 0, 5, 0, 0)
        self.addSubview(btn)
        btns.append(btn)
        return btn
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = Coant.ScreenW / CGFloat(btns.count)
        let h = self.height
        var x : CGFloat = 0
        let y : CGFloat = 0
        
        for  var i = 0;i < btns.count;i++ {
            let btn = btns[i]
            x = CGFloat(i) * w
            btn.frame = CGRectMake(x, y, w, h)
        }
        var i = 0
        for div in divideVs {
            let btn = btns[i+1]
            div.x = btn.x
            i++
        }
    }
    
    func setStatus(){
        //statusFrame.status.reposts_count = 100000
        setBtn(statusFrame.status.reposts_count, btn: retweet)
        setBtn(statusFrame.status.comments_count, btn: comment)
        setBtn(statusFrame.status.attitudes_count, btn: unlike)
    
    }
    
    func setBtn(count: Int,btn: UIButton) {
        if count != 0 {
            btn.setTitle( "\(count)", forState: UIControlState.Normal)
        }
    }
}

